package com.vcredit.creditcat.web.api;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vcredit.creditcat.api.IProductApplyManager;
import com.vcredit.creditcat.dto.product.AuditParamDto;
import com.vcredit.creditcat.dto.product.BlackParamDto;
import com.vcredit.creditcat.dto.product.CreditSubmitDto;
import com.vcredit.creditcat.dto.product.ProductDecisionResultDto;
import com.vcredit.creditcat.vo.JsonResult;
import com.vcredit.framework.annotation.InterfaceRequest;

@RestController
@RequestMapping("creditcat")
public class ExternalClientController extends BaseCreditCatController {
    
    @Reference(version = "1.0")
    private IProductApplyManager productApplyManager;
    
    @InterfaceRequest("计算信用猫提交信用项的授信结果")
    @RequestMapping("/{product}/decision")
    public JsonResult<ProductDecisionResultDto> computeAuditResult(@PathVariable("product") String productName,@RequestBody @Valid CreditSubmitDto paramDto,
                                                                   HttpServletRequest request) throws Exception {
        ProductDecisionResultDto computeAuditResult = productApplyManager.computeAuditResult(productName, paramDto);
        return JsonResult.SuccessResponse(computeAuditResult);
        
    }
    
    @InterfaceRequest("查询信用猫提交信用项的授信结果")
    @RequestMapping("/{product}/inquiry")
    public JsonResult<ProductDecisionResultDto> queryAuditResult(@PathVariable("product") String productName,@RequestBody @Valid AuditParamDto auditParamDto,
        HttpServletRequest request) throws Exception {
        ProductDecisionResultDto computeAuditResult = productApplyManager.queryAuditResult(productName, auditParamDto.getIdentityNo());
        return JsonResult.SuccessResponse(computeAuditResult);
        
    }
    
    @InterfaceRequest("是否允许申请产品")
    @RequestMapping("/{product}/accessible")
    public JsonResult<Boolean> canApply(@PathVariable("product") String productName,@RequestBody BlackParamDto paramDto,
        HttpServletRequest request) throws Exception {
        Boolean canApply = productApplyManager.canApply(productName, paramDto);
        if (canApply) {
            return JsonResult.SuccessResponse(canApply);
        }else {
            return JsonResult.ErrorResponse("不能申请");
        }
    }

}
